//
//  ClassTableViewCell.swift
//  scfriend
//
//  Created by wulanzhou on 2017/7/25.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

import UIKit
import Kingfisher

class ClassTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.lineLabel.backgroundColor = UIColor.withHex(hexString: "00aeed") //防止选中时，放到上面的view消失
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        self.lineLabel.backgroundColor = UIColor.withHex(hexString: "00aeed")//防止高亮时，放到上面的view消失
    }
    
    override var frame: CGRect {
    
        get{ return super.frame }
        set(newFrame){
        
            var frame = newFrame
            frame.origin.x = 10 //cell距离左边距离
            frame.size.width -= 2*frame.origin.x
            frame.size.height -= 10//cell与cell之间的间隔大小
            super.frame = frame
        }
    }
    
    var cellIndexRow:NSInteger = -1
    
    var scClass:SCClass? {
    
        didSet{
            
            let course:SCCourse? = self.scClass?.subjects?[self.cellIndexRow]
            
            let course_name:String = (course?.name)!
            let sep:String = " "
            let stu_num:String = "\(self.scClass?.studentNum ?? "0")个学生"
            
            let txt = NSMutableAttributedString.init(string: course_name + sep + stu_num)
            //设置字体颜色
            txt.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSMakeRange(0,course_name.characters.count))
            txt.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 14.0), range: NSMakeRange(0,course_name.characters.count))
            
            txt.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.withRGBA(159, 159, 159), range: NSMakeRange(course_name.characters.count + sep.characters.count,stu_num.characters.count))
            txt.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 12.0), range: NSMakeRange(course_name.characters.count + sep.characters.count,stu_num.characters.count))
            
            self.textLabel?.attributedText = txt
            
            let scKey:String = self.scClass?.grade ?? "13"
            let detailStr:String = "老师：王老师 学段：\(GradeLevel[scKey] ?? "其他")"
            let attrText = NSMutableAttributedString.init(string: detailStr)
            //设置字体颜色
            attrText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.withRGBA(159, 159, 159), range: NSMakeRange(0,detailStr.characters.count))
            //文字字体
            attrText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 12.0), range: NSMakeRange(0,detailStr.characters.count))
            self.detailTextLabel?.attributedText = attrText
            
            //加载图片
            let url = URL(string: kSC_IMAGE_URL + (course?.icon)!)
            
            _ = self.imageView?.kf.setImage(with: url,
                                            placeholder: nil,
                                            options: [.transition(ImageTransition.fade(1))],
                                            progressBlock: nil,
                                            completionHandler: { image, error, cacheType, imageURL in
                                                
                                                self.imageView?.image = image
                                                let itemSize = CGSize(width: 46.0, height: 46.0)
                                                UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale)
                                                let imageRect = CGRect(x: 0, y: 0, width: itemSize.width, height: itemSize.height)
                                                self.imageView?.image?.draw(in: imageRect)
                                                self.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()
                                                UIGraphicsEndImageContext()
                                                //图像视图可能已经因为新图像而改变了尺寸
                                                //所以需要重新调整单元格的布局
                                                self.setNeedsLayout()
            })
        
        }
    
    }
    
   
    
    
    
    private lazy var lineLabel:UILabel = {
    
        let lab:UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 3, height: self.bounds.size.height))
        lab.backgroundColor = UIColor.withHex(hexString: "00aeed")
        
        return lab
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
        self.layer.cornerRadius = 8.0
        self.layer.masksToBounds = true
        self.clipsToBounds = true
        self.addSubview(self.lineLabel)
    
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.lineLabel.height = self.height
    }
    
   
    
    

}
